package com.retail.lottery.colalottery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.retail.lottery.colalottery.entity.ColaLotteryParam;
import com.retail.lottery.colalottery.enums.DeleteStatusEnum;
import com.retail.lottery.colalottery.service.ColaLotteryParamService;
import com.retail.lottery.colalottery.mapper.ColaLotteryParamMapper;
import com.retail.lottery.colalottery.vo.ResultVO;
import com.retail.lottery.colalottery.vo.request.ColaParamRequest;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author jarvis.zhang
* @description 针对表【cola_lottery_param(可乐抽奖活动配置表)】的数据库操作Service实现
* @createDate 2025-06-13 16:35:30
*/
@Service
public class ColaLotteryParamServiceImpl extends ServiceImpl<ColaLotteryParamMapper, ColaLotteryParam>
    implements ColaLotteryParamService{

    @Override
    public Object queryColaParam(ColaParamRequest request) {
        return baseMapper.selectList(new LambdaQueryWrapper<ColaLotteryParam>()
                .eq(ColaLotteryParam::getBuCode, request.getBuCode())
                .eq(ColaLotteryParam::getSetupId, request.getSetupId())
                .eq(ColaLotteryParam::getIsDel, DeleteStatusEnum.no_del.getCode())
                .in(CollectionUtils.isNotEmpty(request.getParamKey()), ColaLotteryParam::getParamKey, request.getParamKey()));
    }

    @Transactional
    @Override
    public Object addColaParam(ColaParamRequest request) {
        if(CollectionUtils.isEmpty(request.getColaParamList())){
            return ResultVO.succ(null);
        }
        Map<String, ColaParamRequest.ColaParam> paramMap = request.getColaParamList()
                .stream().collect(Collectors.toMap(ColaParamRequest.ColaParam::getParamKey, Function.identity()));
        //1.查询当前是否有同setupId+paramKey数据，存在就删除
        this.lambdaUpdate()
                .eq(ColaLotteryParam::getBuCode, request.getBuCode())
                .eq(ColaLotteryParam::getSetupId, request.getSetupId())
                .eq(ColaLotteryParam::getIsDel, DeleteStatusEnum.no_del.getCode())
                .in(ColaLotteryParam::getParamValue, paramMap.keySet())
                .set(ColaLotteryParam::getIsDel, DeleteStatusEnum.del.getCode())
                .update();
        //2.插入新增的信息
        List<ColaLotteryParam> lotteryParams = Lists.newArrayList();
        request.getColaParamList().forEach(x -> {
            ColaLotteryParam param = new ColaLotteryParam();
            param.setBuCode(request.getBuCode());
            param.setSetupId(request.getSetupId());
            param.setParamKey(x.getParamKey());
            param.setParamValue(x.getParamValue());
            param.setIsDel(DeleteStatusEnum.no_del.getCode());
            lotteryParams.add(param);
        });
        return this.saveBatch(lotteryParams);
    }

}




